import _objectSpread from "@babel/runtime/helpers/esm/objectSpread2";
import _isRegExp from "lodash/isRegExp";
import _isFunction from "lodash/isFunction";
import _forEach from "lodash/forEach";
import pathToRegexp from 'path-to-regexp';

function getRelation(str1, str2) {
  if (str1 === str2) {
    console.warn('Two path are equal!'); // eslint-disable-line
  }

  var arr1 = str1.split('/');
  var arr2 = str2.split('/');

  if (arr2.every(function (item, index) {
    return item === arr1[index];
  })) {
    return 1;
  } else if (arr1.every(function (item, index) {
    return item === arr2[index];
  })) {
    return 2;
  }

  return 3;
}

function getRenderArr(routes) {
  var renderArr = [];
  renderArr.push(routes[0]);

  var _loop = function _loop(i) {
    var isAdd = false; // 是否包含

    isAdd = renderArr.every(function (item) {
      return getRelation(item, routes[i]) === 3;
    }); // 去重

    renderArr = renderArr.filter(function (item) {
      return getRelation(item, routes[i]) !== 1;
    });

    if (isAdd) {
      renderArr.push(routes[i]);
    }
  };

  for (var i = 1; i < routes.length; i += 1) {
    _loop(i);
  }

  return renderArr;
}
/**
 * Get router routing configuration
 * { path:{name,...param}}=>Array<{name,path ...param}>
 * @param {string} path
 * @param {routerData} routerData
 */


export function getRoutes(path) {
  var routerData = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {};

  /* FIXME: START 会选到不是直接子路由 */
  // let routes = Object.keys(routerData).filter(
  //   routePath => routePath.indexOf(path) === 0 && routePath !== path
  // );
  var routes = []; // 匹配到的 路由的 path。相当于 match.path

  var routerPath = ''; // 匹配到的 路由的 path 的 开始正则。
  // 用来找到 当前 path 的路由 和 所有子路由

  var routerPathRegexpStart;
  var routerKeys = Object.keys(routerData);
  routerKeys.find(function (routePath) {
    var route = routerData[routePath];

    if (route.pathRegexp.test(path)) {
      routerPath = routePath;
      routerPathRegexpStart = pathToRegexp(routerPath, [], {
        end: false
      });
      return true;
    }

    return false;
  });

  if (_isRegExp(routerPathRegexpStart)) {
    routerKeys.forEach(function (routePath) {
      if (routerPath !== routePath && routerPathRegexpStart.test(routePath)) {
        routes.push(routePath);
      }
    });
    routes = routes.sort(function (key1, key2) {
      var route2 = routerData[key2];
      var route1 = routerData[key1];

      if ((route2 === null || route2 === void 0 ? void 0 : route2.index) === undefined && (route1 === null || route1 === void 0 ? void 0 : route1.index) === undefined) {
        return 0;
      }

      if (route2 === null || route2 === void 0 ? void 0 : route2.index) {
        return 1;
      }

      if (route1 === null || route1 === void 0 ? void 0 : route1.index) {
        return -1;
      }

      return 0;
    });
  }
  /* FIXME: END */
  // Replace path to '' eg. path='user' /user/name => name


  routes = routes.map(function (item) {
    return item.replace(path, '');
  }); // Get the route to be rendered to remove the deep rendering

  var renderArr = getRenderArr(routes); // Conversion and stitching parameters

  var renderRoutes = renderArr.map(function (item) {
    var exact = !routes.some(function (route) {
      return route !== item && getRelation(route, item) === 1;
    });
    return _objectSpread(_objectSpread({
      exact: exact
    }, routerData["".concat(path).concat(item)]), {}, {
      key: "".concat(path).concat(item),
      path: "".concat(path).concat(item)
    });
  });
  return renderRoutes.sort(function (a, b) {
    return (a.loadIndex || 999) - (b.loadIndex || 999);
  });
}
/**
 * Get router routing configuration
 * { path:{name,...param}}=>Array<{name,path ...param}>
 * @param {String} path
 * @param {Object} [routerData={}]
 */

export function getRoutesContainsSelf(path) {
  var routerData = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {};
  var routes = []; // 匹配到的 路由的 path。相当于 match.path

  var routerPath = ''; // 匹配到的 路由的 path 的 开始正则。
  // 用来找到 当前 path 的路由 和 所有子路由

  var routerPathRegexpStart;
  var routerKeys = Object.keys(routerData);
  routerKeys.find(function (routePath) {
    var route = routerData[routePath];

    if (route.pathRegexp.test(path)) {
      routerPath = routePath;
      routerPathRegexpStart = pathToRegexp(routerPath, [], {
        end: false
      });
      return true;
    }

    return false;
  });

  if (_isRegExp(routerPathRegexpStart)) {
    routerKeys.forEach(function (routePath) {
      if (routerPathRegexpStart.test(routePath)) {
        if (routePath.startsWith(routerPath)) {
          var route = routerData[routePath];
          routes.push(route);
        }
      }
    });
    routes = routes.sort(function (item1, item2) {
      if (item1.notTrueRoute && !item2.notTrueRoute) {
        return -1;
      } else if (!item1.notTrueRoute && item2.notTrueRoute) {
        return 1;
      }

      return 0;
    });
  } // 所有匹配到的路由的 path


  var routePaths = [];

  _forEach(routes, function (route) {
    routePaths.push(route.path);
  });

  routePaths = routePaths.map(function (item) {
    return item.replace(routerPath, '');
  }); // Get the route to be rendered to remove the deep rendering

  var renderArr = getRenderArr(routePaths); // Conversion and stitching parameters

  var renderRoutes = renderArr.map(function (item) {
    var exact = !routePaths.some(function (route) {
      return route !== item && getRelation(route, item) === 1;
    });
    return _objectSpread(_objectSpread({
      exact: exact
    }, routerData["".concat(routerPath).concat(item)]), {}, {
      key: "".concat(routerPath).concat(item),
      path: "".concat(routerPath).concat(item)
    });
  });
  return renderRoutes.sort(function (a, b) {
    return (a.loadIndex || 999) - (b.loadIndex || 999);
  });
}
/**
 * getModuleRouters - 获取模块路由的方法
 * @param {!object} app - dva.app对象
 * @param {Array<Object>} [modules=[]] - 模块路由方法集合
 * @return {Object} - 返回模块路由对象集合
 */

export function getModuleRouters(app) {
  var modules = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : [];
  var routers = {};
  modules.forEach(function (n) {
    routers = Object.assign(routers, _isFunction(n.getRouterData) ? n.getRouterData(app) || {} : {});
  });
  return routers;
}
/**
 * 获取 require('moduleName') 或者 import('moduleName') 的真正的导出对象
 * @param {object} module
 */

export function resolveRequire(module) {
  return module && module.__esModule ? module.default : module;
}